import { createRouter, createWebHistory } from 'vue-router';
import MyLayout from '@/components/MyLayout/index.vue';
import NotFound from '@/components/404/index.vue';
import Login from '@/views/Login.vue';
import { ElMessage } from "element-plus"; // 引入登录组件

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { hidden: true }
    },
    {
        path: '/',
        name: 'Home',
        meta: { hidden: true },
        redirect: '/login' // 默认重定向到登录页面
    },

    {
        path: '/:pathMatch(.*)*', // 新的捕获所有路由的方式
        name: 'notFound',
        meta: { hidden: true },
        component: NotFound
    },

    {
        path: '/competitionManagement',
        name: 'CompetitionManagement',
        component: MyLayout,
        redirect: "/competitionManagement/listAddTeachers",
        meta: {
            title: "比赛管理",
            hidden: false,
            leaf: false,
            requiresAuth: true, // 需要登录才能访问
            roles: ['科创部', '超级管理员'],
            // positions: ['3']
        },
        children: [
            {
                path: "list",
                name: "CompetitionList",
                meta: {
                    title: "全部比赛列表",
                    roles: [ '超级管理员'],
                    // positions: ['3']
                },
                component: () => import('@/views/competitionManagement/CompetitionManagementSuper.vue')
            },
            {
                path: "listAddTeachers",
                name: "CompetitionListAddTeachers",
                meta: {
                    title: "自己增加的比赛列表",
                    roles: ['科创部', '超级管理员'],
                    // positions: ['3']
                },
                component: () => import('@/views/competitionManagement/CompetitionManagementSuperAddTeachers.vue')
            },
            {
                path: "addCompetition",
                name: "addCompetition",
                meta: {
                    title: "添加比赛",
                    roles: ['科创部', '超级管理员'],
                    // positions: ['3']
                },
                component: () => import('@/views/competitionManagement/addCompetition.vue')
            }



        ]
    },
    {
        path: '/activityManagement',
        name: 'ActivityManagement',
        component: MyLayout,
        redirect: "/activityManagement/listAddTeachers",
        meta: {
            title: "活动管理",
            hidden: false,
            leaf: false,
            requiresAuth: true, // 需要登录才能访问
            roles: ['学工处', '超级管理员'],
            // positions: ['4']
        },
        children: [
            {
                path: "list",
                name: "ActivityList",
                meta: {
                    title: "全部的活动列表",
                    roles: [ '超级管理员'],
                    // positions: ['4']
                },
                component: () => import('@/views/activityManagement/ActivityManagementSuper.vue')
            },
            {
                path: "listAddTeachers",
                name: "ActivityListAddTeachers",
                meta: {
                    title: "自己发布的活动列表",
                    roles: ['超级管理员', '学工处'],
                    // positions: ['4']
                },
                component: () => import('@/views/activityManagement/ActivityManagementAddTeachers.vue')
            }
            ,
            {
                path: "addActivity",
                name: "addActivity",
                meta: {
                    title: "添加活动",

                },
                component: () => import('@/views/activityManagement/addActivity.vue')
            }


        ],


    },



    // 新增的反馈管理路由
    {
        path: '/feedbackManagement',
        name: 'FeedbackManagement',
        component: MyLayout,
        redirect: "/feedbackManagement/list",
        meta: {
            title: "反馈管理",
            hidden: false,
            leaf: true,
            requiresAuth: true, // 需要登录才能访问
            roles: ['超级管理员'], // 只有超级管理员可以访问
            // positions: ['4']
        },
        children: [
            {
                path: "list",
                name: "FeedbackList",
                meta: {
                    title: "反馈列表",
                    roles: ['超级管理员'], // 只有超级管理员可以访问
                    // positions: ['4']
                },
                component: () => import('@/views/feedbackManagement/FeedbackManagementSuper.vue') // 替换为实际的反馈管理组件
            }
        ]
    },
    {
        path: '/lifeManagement',
        name: 'LifeManagement',
        component: MyLayout,
        redirect: "/lifeManagement/life",
        meta: {
            title: "生活管理",
            hidden: false,
            leaf: false,
            requiresAuth: true, // 需要登录才能访问
            // positions: ['3']
        },
        children: [
            {
                path: "list",
                name: "LifeList",
                meta: {
                    title: "全部生活列表",
                    // positions: ['3']
                },
                component: () => import('@/views/lifeManagement/LifeManagementSuper.vue')
            }

        ]
    }


    ,

    // 新增的祝福管理路由
    {
        path: '/blessManagement',
        name: 'BlessManagement',
        component: MyLayout,
        redirect: "/blessManagement/list",
        meta: {
            title: "祝福管理",
            hidden: false,
            leaf: false,
            requiresAuth: true, // 需要登录才能访问
        },
        children: [
            {
                path: "list",
                name: "BlessList",
                meta: {
                    title: "祝福列表",
                },
                component: () => import('@/views/blessManagement/BlessManagementSuper.vue')
            }

        ]
    },
    {
        path: '/employeeManagement',
        name: 'EmployeeManagement',
        component: MyLayout,
        redirect: "/employeeManagement/list",
        meta: {
            title: "权限管理",
            hidden: false,
            leaf: false,
            requiresAuth: true, // 需要登录才能访问

        },
        children: [
            {
                path: "list",
                name: "EmployeeList",
                meta: {
                    title: "人员管理",

                },
                component: () => import('@/views/employeeManagement/EmployeeManagement.vue')
            },
            {
                path: 'role',
                name: 'role',
                component: () => import('@/views/ums/role/roleIndex'),
                meta: {title: '角色列表', icon: 'ums-role'}
            },
            {
                path: 'allocMenu',
                name: 'allocMenu',
                component: () => import('@/views/ums/role/allocMenu'),
                meta: {title: '分配菜单'},
                hidden: true
            },
            {
                path: 'allocResource',
                name: 'allocResource',
                component: () => import('@/views/ums/role/allocResource'),
                meta: {title: '分配资源'},
                hidden: true
            },
            {
                path: 'menu',
                name: 'menu',
                component: () => import('@/views/ums/menu/index'),
                meta: {title: '菜单列表', icon: 'ums-menu'}
            },
            {
                path: 'addMenu',
                name: 'addMenu',
                component: () => import('@/views/ums/menu/add'),
                meta: {title: '添加菜单'},
                hidden: true
            },
            {
                path: 'updateMenu',
                name: 'updateMenu',
                component: () => import('@/views/ums/menu/update'),
                meta: {title: '修改菜单'},
                hidden: true
            },
            {
                path: 'resource',
                name: 'resource',
                component: () => import('@/views/ums/resource/resourceIndex'),
                meta: {title: '资源列表', icon: 'ums-resource'}
            },
            {
                path: 'resourceCategory',
                name: 'resourceCategory',
                component: () => import('@/views/ums/resource/categoryList'),
                meta: {title: '资源分类'},
                hidden: true
            }
        ]
    }


];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
});

router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('isAuthenticated'); // 使用 'isAuthenticated' 作为登录状态的标志

    if (to.meta.requiresAuth) {
        // 如果目标路由需要登录
        if (isAuthenticated) {
            // 如果已登录，继续导航
            next();
        } else {
            // 如果未登录，重定向到登录页面
            next({ name: 'Login' });
            ElMessage.error('您未登录，请先登录！');
        }
    } else {
        // 如果目标路由不需要登录，继续导航
        next();
    }
});

export default router;